
package xj.graph2d.app;

public class Animator extends Thread {

  private AnimationListener listener;

  private static int interval = 50; // 50 milli-seconds 
  
  /**
   * Starts the animation thread.
   */
  public Animator(AnimationListener listener) {
    this(50, listener);
  }

  public Animator(int interval, AnimationListener listener) {
    super("----thread: animator----");
    this.interval = interval;
    this.listener = listener;
    setDaemon(true);
    start();

    System.out.println("Start Animator " + listener.getClass().getName());
  }

  /**
   * Returns the intervale, in seconds, between each save.
   */
  public static int getInterval() {
    return interval;
  }

  /**
   * Sets the interval, in seconds, between each save.
   */

  public static void setInterval(int newInterval) {
    interval = newInterval;
  }

  @Override
  public void run() {
    if (interval == 0)
      return;

    while(true) {
      try {
        Thread.sleep(interval);
      }	catch(InterruptedException i) {
        return;
      }

      if (listener != null) { 
	listener.doAction();
      }

      if (interrupted())
        return;
    }
  }
  

}
